package cn.tedu.emp.comtroller;

import cn.HK.po.Emp;
import cn.HK.utils.CookieUtils;
import cn.HK.vo.SystemResult;
import cn.tedu.emp.service.ServiceLogin;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/emp")
public class ControllerLogin {
    @Autowired
    private ServiceLogin serviceLogin;

    @RequestMapping("/login")
    public SystemResult emplogin(Emp emp,
                                 HttpServletResponse response,
                                 HttpServletRequest request){
        //从业务层拿到key值 ticket
        System.out.println(emp.geteUsername()+emp.getePassword());

        String ticket = serviceLogin.doLogin(emp);
        if (StringUtils.isEmpty(ticket)){
            //ticket为null或"" 进入if 相当于没有存储redis 登录失败
            return SystemResult.build(201,"用户名密码错误",null);
        }
            //ticket不为空,登录成功,写入response一个cookie值
        CookieUtils.setCookie
                (request,response,"HK_TICKET_",ticket);
            return SystemResult.ok();
    }
     /*
        查询登录状态
        请求方式:get
        请求参数:ticket
        返回值类型:Emp
     */
    @RequestMapping(value = "/status", method = RequestMethod.GET)
    public Emp getEmpJson(String ticket){
        //拿到ticket"票",将其封装成Json字符串
        Emp emp = serviceLogin.getEmpJson(ticket);
        return emp;
    }
}
